<template>
  <el-dialog
    title="编辑术语信息"
    :visible.sync="open"
    width="600px"
    append-to-body
    @close="closeDialog"
    class="business-dialog"
    :close-on-click-modal="false"
  >
    <el-form :model="formModel" ref="form" label-width="auto" size="small" label-suffix=":" :rules="rules">
      <el-row :gutter="20">
        <el-col :span="24">
          <el-form-item label="标准编号" prop="standardNo">{{ formModel.standardNo }}</el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="版本" prop="standardVersion">
            <span>{{ formModel.standardVersion }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="标准名称" prop="standardName">
            <span>{{ formModel.standardName }}</span>
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="中文名称" prop="nameZh">
            <el-input v-model="formModel.nameZh" placeholder="请输入" clearable maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="英文名称" prop="nameEn">
            <el-input v-model="formModel.nameEn" placeholder="请输入" clearable maxlength="100" />
          </el-form-item>
        </el-col>
        <el-col :span="24">
          <el-form-item label="缩略词" prop="nameShort">
            <el-input v-model="formModel.nameShort" placeholder="请输入" clearable maxlength="100" />
          </el-form-item>
        </el-col>
      </el-row>
    </el-form>
    <div slot="footer" class="dialog-footer">
      <el-button size="small" @click="open = false">取消</el-button>
      <el-button type="primary" :loading="loading" @click="handleSubmit" size="small">确定</el-button>
    </div>
  </el-dialog>
</template>
<script>
import { Message } from 'element-ui'
import { updateTermInfo } from '@/api/business'
import { cloneDeep } from 'lodash'

export default {
  name: 'TermUpdateModal',
  props: {
    updateData: {
      type: Object,
      default: () => ({})
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  watch: {
    visible(val) {
      this.open = val
      if (val) {
        this.formModel = cloneDeep(this.updateData)
      }
    }
  },
  data() {
    return {
      open: this.visible,
      formModel: {},
      loading: false,
      rules: {
        nameShort: [{ required: true, message: '请输入缩略词', trigger: 'blur' }],
        nameZh: [{ required: true, message: '请输入中文', trigger: 'blur' }],
        nameEn: [{ required: true, message: '请输入英文', trigger: 'blur' }]
      }
    }
  },
  methods: {
    closeDialog() {
      this.$emit('update:visible', false)
    },
    handleSubmit() {
      this.$refs.form.validate(async (valid) => {
        if (!valid) return
        this.loading = true
        try {
          const { jsonContent, ...rest } = this.formModel
          const res = await updateTermInfo(rest)
          if (res.data) {
            this.open = false
            this.closeDialog()
            this.$emit('success')
            return Message.success('修改成功')
          }
          throw new Error()
        } catch (error) {
          Message.error('修改失败')
        } finally {
          this.loading = false
        }
      })
    }
  }
}
</script>
